'''
Created on 1 nov 2012

@author: Lanboost
'''

from CFilter import *
import Image
import CPerlinNoise
import ImageLibr

class CFilterBubbles(CFilter):
    
    
    def applyFilter(self, aImage):
        new_image = Image.new("RGB", aImage.size)
        new_image_data = []
        
        pixels = aImage.getdata()
        step = 255/aImage.size[0]
        rain = step
        for pixel in pixels:
            r = int(pixel[0])
            g = int(pixel[1])
            b = int(pixel[2])
            new_pixel = (r,g,b)
            new_image_data.append(new_pixel)
        
        imgWidth = aImage.size[0]
        imgHeight = aImage.size[1]
        
        image_noise_1 = CPerlinNoise.create((imgWidth/5+1, imgHeight/5+1))
        image_noise_2 = CPerlinNoise.create((imgWidth/5+1, imgHeight/5+1))
        image_noise_3 = CPerlinNoise.create((imgWidth/5+1, imgHeight/5+1))
        image_noise_data_1 = list(image_noise_1.getdata())
        image_noise_data_2 = list(image_noise_2.getdata())
        image_noise_data_3 = list(image_noise_3.getdata())
        for x in range(0, imgWidth):
            for y in range(0, imgHeight):
                pixel = ImageLibr.getPixel(aImage, new_image_data, x, y)
                r = int(pixel[0])
                g = int(pixel[1])
                b = int(pixel[2])
                
                
                pixel1 = ImageLibr.getPixel(image_noise_1, image_noise_data_1, x//5, y//5)
                pixel2 = ImageLibr.getPixel(image_noise_2, image_noise_data_2, x//5, y//5)
                pixel3 = ImageLibr.getPixel(image_noise_3, image_noise_data_3, x//5, y//5)
                #v = int(r+0*g+0*b)
                #print(str(r)+","+str(g)+","+str(b))
                new_pixel = None
                #if c < 255:
                #print(rp, r,  rp*r)
                mul = 100.0
                
                """print((pixel1/mul))
                if x < 300:
                    new_pixel = (int(r*(pixel1/mul)),0,0)
                elif x >= 300 and x < 600:
                    new_pixel = (0,int(g*(pixel2/mul)),0)
                elif x >= 600:
                     new_pixel = (0,0, int(b*(pixel3/mul)))"""
                new_pixel = (int(r*(pixel1/mul)), int(g*(pixel2/mul)), int(b*(pixel3/mul)))
                #else:
                #   new_pixel = (r, 255, 255)
                new_image_data[y*aImage.size[0]+x] = new_pixel
        
            
                
            
        
        new_image.putdata(new_image_data)
        return new_image
    
    